using System;
using System.Collections.Generic;
using System.Linq;
using Voodoo.Sauce.Internal.Extension;

namespace Voodoo.Sauce.Internal.Analytics
{
	[Serializable]
	public class ConversionEventsSettings
	{
		private const int EVENT_NAME_DATA_INDEX = 0;

		private const int EVENT_TRIGGER_DATA_INDEX = 1;

		private const int EVENT_TOKEN_DATA_INDEX = 2;

		private const char DATA_SEPARATOR = '/';

		public bool UseConversionEvents;

		public int DaysUntilExpiration;

		public ConversionAdEventInfo[] AdEvents;

		public ConversionEventInfo[] UnknownEvents;

		public bool IsEnabled()
		{
			if (UseConversionEvents)
			{
				return HasAnyEvents();
			}
			return false;
		}

		public bool HasAnyEvents()
		{
			ConversionAdEventInfo[] adEvents = AdEvents;
			if (adEvents == null || !adEvents.Any())
			{
				return UnknownEvents?.Any() ?? false;
			}
			return true;
		}

		public bool HasKnownEvents()
		{
			return AdEvents?.Any() ?? false;
		}

		public bool HasValidExpirationDate()
		{
			return DaysUntilExpiration > 0;
		}

		public List<string> GetInvalidEventsData()
		{
			List<string> list = new List<string>();
			if (UnknownEvents != null)
			{
				list.AddRange(UnknownEvents.Select((ConversionEventInfo eventInfo) => eventInfo.OriginalData));
			}
			if (AdEvents != null)
			{
				list.AddRange(from eventInfo in AdEvents
					where !eventInfo.IsValid()
					select eventInfo.OriginalData);
			}
			return list;
		}

		public List<string> GetDuplicatedEventsData()
		{
			List<string> list = new List<string>();
			if (AdEvents != null)
			{
				list.AddRange(from eventInfo in AdEvents
					group eventInfo by eventInfo.OriginalData into grouping
					where grouping.Count() > 1
					select grouping.Key);
			}
			return list;
		}

		public static ConversionEventsSettings Build(bool useConversionEvents, string daysUntilExpirationValue, IEnumerable<string> eventsValues)
		{
			List<ConversionAdEventInfo> list = new List<ConversionAdEventInfo>();
			List<ConversionEventInfo> list2 = new List<ConversionEventInfo>();
			foreach (string eventsValue in eventsValues)
			{
				if (!string.IsNullOrWhiteSpace(eventsValue))
				{
					ConversionEventInfo conversionEventInfo = BuildEvent(eventsValue.RemoveSpace());
					if (conversionEventInfo is ConversionAdEventInfo item)
					{
						list.Add(item);
					}
					else
					{
						list2.Add(conversionEventInfo);
					}
				}
			}
			int.TryParse(daysUntilExpirationValue?.RemoveSpace(), out var result);
			return new ConversionEventsSettings
			{
				UseConversionEvents = useConversionEvents,
				DaysUntilExpiration = result,
				AdEvents = (list.Any() ? list.ToArray() : null),
				UnknownEvents = (list2.Any() ? list2.ToArray() : null)
			};
		}

		private static ConversionEventInfo BuildEvent(string eventValue)
		{
			try
			{
				string[] array = eventValue.Split('/');
				ConversionEventName conversionEventName = ParseEventName(array[0]);
				string adjustEventToken = array[2];
				switch (conversionEventName)
				{
				case ConversionEventName.FS:
				case ConversionEventName.RV:
				{
					int.TryParse(array[1], out var result);
					return new ConversionAdEventInfo
					{
						OriginalData = eventValue,
						Name = conversionEventName,
						AdjustEventToken = adjustEventToken,
						TargetNumberAdsWatched = result
					};
				}
				case ConversionEventName.Unknown:
					return new ConversionEventInfo
					{
						OriginalData = eventValue,
						Name = conversionEventName,
						AdjustEventToken = adjustEventToken
					};
				}
			}
			catch
			{
			}
			return new ConversionEventInfo
			{
				Name = ConversionEventName.Unknown,
				OriginalData = eventValue
			};
		}

		private static ConversionEventName ParseEventName(string value)
		{
			if (!Enum.TryParse<ConversionEventName>(value, ignoreCase: true, out var result))
			{
				return ConversionEventName.Unknown;
			}
			return result;
		}
	}
}
